import request from "@/utils/request";

// export const userInfoService = () => request.get('/api/auth/userInfo')

export const userInfoService = (userId) => {
    return request({
        url: '/api/profile/info',
        method: 'get',
        params : {userId}
    })
};



export const updateUserInfoService = userInfoDTO =>
  request.put('/api/profile/update', userInfoDTO)

export const deleteAccountService = (id) => {
    return request({
        url: '/api/user-account/delete',
        method: 'delete',
        params : {id}
    })
};

export const resetPasswordService = (id, password, repassword ) => {
  return request({
        url: '/api/user-account/reset-password',
        method: 'put',
        params : {id, password, repassword}
    })
};


export const getCartService = (page, size, userId ) => {
  return request({
        url: '/api/cart-item/get',
        method: 'get',
        params : {page, size, userId}
    })
};

export const removeCartItemService = (id ) => {
  return request({
        url: '/api/cart-item/delete',
        method: 'delete',
        params : {id}
    })
};

export const addCartService = (data) => {
  return request({
    url: '/api/cart-item/add',
    method: 'post',
    data  // 使用data传递JSON对象
  });
}

export const getOrdersService = (page, size, userId ) => {
  return request({
        url: '/api/order-main',
        method: 'get',
        params : {page, size, userId}
    })
};

export function payOrderService(orderId) {
  return request({
    url: '/api/order-main/pay',
    method: 'post',
    params: { orderId }
  })
}

export function cancelOrderService(orderId) {
  return request({
    url: '/api/order-main/cancel',
    method: 'post',
    params: { orderId }
  })
}

export function refundOrderService(orderId) {
  return request({
    url: '/api/order-main/refund',
    method: 'post',
    params: { orderId }
  })
}

export function deleteOrderService(orderId) {
  return request({
    url: '/api/order-main/delete',
    method: 'post',
    params: { orderId }
  })
}

export function getWalletService( id ) {
  return request({
    url: '/api/profile/wallet',
    method: 'get',
    params: { id }
  })
}


export function rechargeWalletService(orderId) {
  return request({
    url: '/api/',
    method: 'post',
    params: {  }
  })
}

export function getMembershipService(page, size) {
  return request({
    url: '/api/profile/getMember',
    method: 'post',
    params : {page, size}
  })
}

export function getMemberValidityService(page, size) {
  return request({
    url: '/api/profile/memValidity',
    method: 'get',
    params : {page, size}
  })
}

// 会员升级相关接口（保持与现有代码相同格式）
export const getUpgradeOptionsService = () => {
  return request({
    url: '/api/profile/upgrade-options',
    method: 'get'
  })
}

export const upgradeMemberService = (targetLevel, months) => {
  const formData = new URLSearchParams();
  formData.append('targetLevel', targetLevel);
  formData.append('months', months);
  // 打印参数
  console.log('传递的 targetLevel 参数值：', targetLevel);
  console.log('传递的 months 参数值：', months);
  console.log('formData 内容：', formData.toString());
  return request({
    url: '/api/profile/upgrade',
    method: 'post',
    data: formData,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    }
  });
}


export const renewMemberService = (orderId, months) => {
  return request({
    url: '/api/profile/renew',
    method: 'post',
    data: { orderId, months }
  })
}




// export function renewMembershipService(orderId) {
//   return request({
//     url: '/api/',
//     method: 'post',
//     params: {  }
//   })
// }

export const executeMemberDowngradeService = (userId) => {
  return request({
    url: '/api/profile/downgrade',
    method: 'post',
    data: { userId }
  })
}

export const exchangePointsToBalance = (data) => {
  return request({
    url: '/api/profile/exchange-points',
    method: 'post',
    params: {  // 改用URL参数传递
      userId: data.userId,
      points: data.points,
      wallet: parseInt(data.wallet) // 字段名改为wallet
    }
  })
}

// 支付成功增加积分
export const addPointsAfterPayment = (orderId, amount, userId) => {
  return request({
    url: '/api/profile/add-from-payment',
    method: 'post',
    data: {
      orderId,
      userId: userId || JSON.parse(localStorage.getItem('userInfo'))?.id,
      points: Math.floor(amount) // 金额取整得积分
    }
  })
}



export function getDeliveryListService(id){
    return request({
        
    })
}

// 提交商品评价
export const submitEvaluationService = (orderItemId, payload) =>
  request.post('/api/evaluation', { orderItemId, ...payload });